package com.kyzh.core.pager.weal.gift

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.dao.MMKVConsts
import com.kyzh.core.R
import com.kyzh.core.pager.weal.gift.GiftItemFragment
import kotlinx.android.synthetic.main.activity_gift.*

class GiftActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_gift)

        MMKVConsts.giftSearch = ""
        initView()
    }

    private fun initView() {

        val adapter = TabAdapter(this)
        viewPager.adapter = adapter
        viewPager.currentItem = 0
        viewPager.isSaveEnabled = true
        val titles = arrayListOf(getString(R.string.standardGift),getString(R.string.vipGift))
        val mediator = TabLayoutMediator(tabLayout, viewPager){ tab, position->
            tab.text = titles[position]
        }
        mediator.attach()

        tabLayout.getTabAt(0)?.customView = null
        val textView2: TextView = View.inflate(this, R.layout.me_text, null) as TextView
        textView2.text = tabLayout.getTabAt(0)?.text
        tabLayout.getTabAt(0)?.customView = textView2
        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                tab.customView = null
                val textView5: TextView = View.inflate(this@GiftActivity, R.layout.me_text, null) as TextView
                textView5.text = tab.text
                tab.customView = textView5
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                tab?.customView = null
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }
        })

        etSearch.setOnEditorActionListener { v, actionId, event ->

            MMKVConsts.giftSearch = etSearch.text.toString()
            viewPager.adapter =adapter
            return@setOnEditorActionListener true
        }
        ivClose.setOnClickListener {
            MMKVConsts.giftSearch = ""
            etSearch.setText("")
            viewPager.adapter =adapter
        }
    }

    private val ids = arrayListOf(1.toLong(),2.toLong())
    private inner class TabAdapter(context: FragmentActivity):FragmentStateAdapter(context){

        private val createdIds = hashSetOf<Long>()
        override fun getItemCount(): Int {
            return ids.size
        }
        override fun getItemId(position: Int): Long {
            return ids[position]
        }

        override fun containsItem(itemId: Long): Boolean {
            return createdIds.contains(itemId)
        }


        override fun createFragment(position: Int): Fragment {
            val id = ids[position]
            createdIds.add(id)
            return GiftItemFragment.newInstance(position)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}